package com.cxhc.system.service.hcdoc.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.cxhc.common.utils.StringUtils;
import com.cxhc.system.domain.hcdoc.HdPetitionAgent;
import com.cxhc.system.domain.hcdoc.HdProjectDocInfo;
import com.cxhc.system.mapper.hcdoc.HdPetitionAgentMapper;
import com.cxhc.system.mapper.hcdoc.HdProjectDocInfoMapper;
import com.cxhc.system.service.hcdoc.IHdPetitionAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 信访信息Service业务层处理
 * 
 * @author Dhq
 * @date 2024-09-26
 */
@Service
public class HdPetitionAgentServiceImpl implements IHdPetitionAgentService
{
    @Autowired
    private HdPetitionAgentMapper hdPetitionAgentMapper;

    @Autowired
    private HdProjectDocInfoMapper hdProjectDocInfoMapper;

    /**
     * 查询信访信息
     * 
     * @param petitionNo 信访信息主键
     * @return 信访信息
     */
    @Override
    public HdPetitionAgent selectHdPetitionAgentByPetitionNo(String petitionNo)
    {
        HdPetitionAgent hdPetitionAgent = hdPetitionAgentMapper.selectHdPetitionAgentByPetitionNo(petitionNo);
        List<HdProjectDocInfo> hdProjectDocInfos = hdProjectDocInfoMapper.selectListByProjectIdAndModule(hdPetitionAgent.getPetitionNo(),"2");
        hdPetitionAgent.setHdProjectDocInfos(hdProjectDocInfos);
        return hdPetitionAgent;
    }

    /**
     * 查询信访信息列表
     * 
     * @param hdPetitionAgent 信访信息
     * @return 信访信息
     */
    @Override
    public List<HdPetitionAgent> selectHdPetitionAgentList(HdPetitionAgent hdPetitionAgent)
    {
        return hdPetitionAgentMapper.selectHdPetitionAgentList(hdPetitionAgent);
    }

    @Override
    public Map<Integer, Long> statisticsByPetitionType(HdPetitionAgent hdPetitionAgent) {
        List<HdPetitionAgent> hdPetitionAgents = hdPetitionAgentMapper.selectHdPetitionAgentList(hdPetitionAgent);
        return hdPetitionAgents.stream().collect(Collectors.groupingBy(HdPetitionAgent::getPetitionType, Collectors.counting()));
    }

    /**
     * 新增信访信息
     * 
     * @param hdPetitionAgent 信访信息
     * @return 结果
     */
    @Override
    public int insertHdPetitionAgent(HdPetitionAgent hdPetitionAgent)
    {

        if (!ObjectUtils.isEmpty(hdPetitionAgentMapper.selectHdPetitionAgentByPetitionNo(hdPetitionAgent.getPetitionNo()))){
            throw new RuntimeException("信访编号已存在");
        }

        int insert = hdPetitionAgentMapper.insertHdPetitionAgent(hdPetitionAgent);
        if (1 == insert){
            List<HdProjectDocInfo> hdProjectDocInfos = hdPetitionAgent.getHdProjectDocInfos();
            if (!CollectionUtils.isEmpty(hdProjectDocInfos)){
                for (HdProjectDocInfo hdProjectDocInfo : hdProjectDocInfos) {
                    //文档所属模块1-项目中心 2-信访件 3-年度资料 4-停车包月 5-小额招标
                    hdProjectDocInfo.setModule("2");
                    hdProjectDocInfo.setProjectInfoId(String.valueOf(hdPetitionAgent.getPetitionNo()));
                }
                hdProjectDocInfoMapper.batchInsertDoc(hdProjectDocInfos);
            }
        }
        return insert;
    }

    /**
     * 修改信访信息
     * 
     * @param hdPetitionAgent 信访信息
     * @return 结果
     */
    @Override
    public int updateHdPetitionAgent(HdPetitionAgent hdPetitionAgent)
    {
        return hdPetitionAgentMapper.updateHdPetitionAgent(hdPetitionAgent);
    }

    /**
     * 批量删除信访信息
     * 
     * @param petitionNos 需要删除的信访信息主键
     * @return 结果
     */
    @Override
    public int deleteHdPetitionAgentByPetitionNos(String[] petitionNos)
    {
        return hdPetitionAgentMapper.deleteHdPetitionAgentByPetitionNos(petitionNos);
    }

    /**
     * 删除信访信息信息
     * 
     * @param petitionNo 信访信息主键
     * @return 结果
     */
    @Override
    public int deleteHdPetitionAgentByPetitionNo(String petitionNo)
    {
        return hdPetitionAgentMapper.deleteHdPetitionAgentByPetitionNo(petitionNo);
    }
}
